var mapa, setMarker = true, geocoder;
var centrePoint = new GLatLng('52.228220003460486', '21.011524200439453');
var points = [];
var firstPoint;
var markersToArea = [];
var polLin;
var markers = [];
var i=1;
var coordinates = [];
var notPolLin = true;


function setUserLocalization(lat, long)
{
    if(GBrowserIsCompatible())
    {
        mapa = new GMap2(document.getElementById('map'),{mapTypes: [G_NORMAL_MAP]});
        mapa.addControl(new GLargeMapControl());
        mapa.addControl(new GOverviewMapControl()); 
        mapa.addControl(new GScaleControl());
        mapa.addControl(new GLargeMapControl());        
        mapa.addControl(new GMapTypeControl())

        if(lat != "" && long != "")
        {
            centrePoint = new GLatLng(lat, long);    
        }
        mapa.setCenter(centrePoint, 14);
        
        var marker = new GMarker(centrePoint, {draggable: true});
        marker.id = i;
        i++;
        markers.push(marker);
        mapa.addOverlay(marker);
        
        // add a drag listener to the map
        GEvent.addListener(marker, "dragend", function() 
        {
            var point = marker.getPoint();
            mapa.panTo(point);
            document.getElementById("lat").value = point.lat();
            document.getElementById("lon").value = point.lng();
        });
    }       
}

function startMap()
{
    if(GBrowserIsCompatible())
    {
        mapa = new GMap2(document.getElementById('map'),{mapTypes: [G_NORMAL_MAP]});
        mapa.addControl(new GLargeMapControl());
        mapa.addControl(new GOverviewMapControl()); 
        mapa.addControl(new GScaleControl());
        mapa.addControl(new GLargeMapControl());        
        mapa.addControl(new GMapTypeControl());
        geocoder = new GClientGeocoder();

        
        mapa.setCenter(centrePoint, 14);

        if(setMarker){
            var marker = new GMarker(centrePoint, {draggable: true});
            marker.id = i;
            i++;
            markers.push(marker);
            mapa.addOverlay(marker);

            // add a drag listener to the map
            GEvent.addListener(marker, "dragend", function()
            {
                var point = marker.getPoint();
                mapa.panTo(point);
                document.getElementById("latitude").value = point.lat();
                document.getElementById("longitude").value = point.lng();
                points.push(new GLatLng(point.lat(),point.lng()));
                //if(points.length == 1)
    //            {
                    firstPoint = new GLatLng(point.lat(),point.lng());
    //            }
                if(points.length >=3)
                {
                    checkIfMarkerIn(1);
                }
            });

        }
    }    
}

function setNewMarker(point)
{
    var mapPoint = point || centerPoint;
    var marker = new GMarker(mapPoint, {draggable: true});
    marker.id = i;
    i++;
    markers.push(marker);
    mapa.addOverlay(marker);
    
    GEvent.addListener(marker, "dragend", function() 
    {
        var point = marker.getPoint();
        mapa.panTo(point);
        //document.getElementById("latitude").value = point.lat();
//        document.getElementById("longitude").value = point.lng();
        points.push(new GLatLng(point.lat(),point.lng()));
        if(points.length == 1)
        {
            firstPoint = new GLatLng(point.lat(),point.lng());   
        }
        if(points.length >=3)
        {
            checkIfMarkerIn(1);   
        }
    });     
}

function addArea()
{
    coordinates.push(firstPoint);    
    var polilinia = new GPolygon(coordinates,'#ff0000',4,0.7,'#ff0000',0.2);
    polLin = polilinia; 
    mapa.addOverlay(polilinia); 
}

function removePoliline()
{
     mapa.removeOverlay(polLin);
}

function checkIfMarkerIn(id)
{
    coordinates = [];
    var first;
    if(notPolLin)
    {
        notPolLin = false;
    }
    else
    {
        removePoliline();    
    }
    $.each(markers, function(index, value) 
    { 
      if(value.id == id)
      {
//        firstPoint = value;    
      }
      
      var point = value.getPoint();
      coordinates.push(new GLatLng(point.lat(),point.lng())); 
    });
    
    addArea();
}